﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 客户端
{
    public partial class 选购界面 : Form
    {
        string constr = login.constr;
        DataSet myst = new DataSet();
        SqlDataAdapter myda;

        public static string Caccount;//从登录界面获取用户账号
        string gid;
        int price;
        int num;
        string Coid;

        public 选购界面()
        {
            InitializeComponent();
            textBox2.Text = "1";
            allselect();
        }

        private void btn_allselect_Click(object sender, EventArgs e)
        {
            allselect();
        }
        public void allselect()
        {
            SqlConnection sqlcon = new SqlConnection(constr);

            try
            {
            string sql = "select Goods.gid as 商品编号 ,Goods.gname as 商品名称 ,Goods.gtype as 商品类型, Goods.gprice as 商品价格, Goods.gammount as 商品数量 ,Supplier.sname as 品牌 from Goods,Supplier where Goods.sid = Supplier.sid";
            SqlCommand cmd = new SqlCommand(sql, sqlcon);
            sqlcon.Open();
            myda = new SqlDataAdapter(cmd);
            myst.Tables.Clear();
            myda.Fill(myst, "Goods,Supplier");
            dataGridView1.DataSource = myst.Tables["Goods,Supplier"];
            }catch(Exception ex)
            {
                MessageBox.Show(ex.ToString()+"失败");
            }
            finally
            {
                sqlcon.Close();
            }

        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text=="商品名称查询")
            {
                using (SqlConnection sqlcon=new SqlConnection(constr))
                {
                    sqlcon.Open();
                    string sql = "select Goods.gid as 商品编号 ,Goods.gname as 商品名称 ,Goods.gtype as 商品类型, Goods.gprice as 商品价格, Goods.gammount as 商品数量 ,Supplier.sname as 品牌 from Goods,Supplier where Supplier.sid=Goods.sid and Goods.gname='" + textBox1.Text.ToString()+"'";
                    SqlCommand sqlcmd = new SqlCommand(sql,sqlcon);
                    myda = new SqlDataAdapter(sqlcmd);
                    myst.Tables.Clear();
                    myda.Fill(myst,"mc");
                    dataGridView1.DataSource = myst.Tables["mc"];
                    sqlcon.Close();
                }
            }
            else if (comboBox1.Text=="商品类型查询")
            {
                using (SqlConnection sqlcon = new SqlConnection(constr))
                {
                    sqlcon.Open();
                    string sql = "select Goods.gid as 商品编号 ,Goods.gname as 商品名称 ,Goods.gtype as 商品类型, Goods.gprice as 商品价格, Goods.gammount as 商品数量 ,Supplier.sname as 品牌 from Goods,Supplier where  Supplier.sid=Goods.sid and Goods.gtype='" + textBox1.Text.ToString()+"'";
                    SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
                    myda = new SqlDataAdapter(sqlcmd);
                    myst.Tables.Clear();
                    myda.Fill(myst, "lx");
                    dataGridView1.DataSource = myst.Tables["lx"];
                    sqlcon.Close();
                }

            }
            else if (comboBox1.Text=="品牌查询")
            {
                using (SqlConnection sqlcon = new SqlConnection(constr))
                {
                    sqlcon.Open();
                    string sql = "select Goods.gid as 商品编号 ,Goods.gname as 商品名称 ,Goods.gtype as 商品类型, Goods.gprice as 商品价格, Goods.gammount as 商品数量 ,Supplier.sname as 品牌 from Goods,Supplier where Supplier.sid=Goods.sid and Supplier.sname='" + textBox1.Text.ToString() + "'";
                    SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
                    myda = new SqlDataAdapter(sqlcmd);
                    myst.Tables.Clear();
                    myda.Fill(myst, "pp");
                    dataGridView1.DataSource = myst.Tables["pp"];
                    sqlcon.Close();
                }
            }
            else
            {
                MessageBox.Show("错误！");
            }
        }
        //添加到购物车
        private void btn_addtoshoppingcar_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("请输入商品数量");
            }
            else if (num < int.Parse(textBox2.Text.ToString()))
            {
                MessageBox.Show("商品数量超出库存值！请选择合理数量");
            }
            else
            {
                SqlConnection sqlcon = new SqlConnection(constr);
                Random ra = new Random();
                Coid = ra.Next().ToString();//购物车表的主键
                string Codate = DateTime.Now.ToLocalTime().ToString(); //获取当前时间
                try
                {
                string sql = "insert into Corder(Coid,Caccount,gid,Codate,Coamount,Costate) values('" + Coid + "','" + Caccount + "','" + gid + "','" + Codate + "'," + int.Parse(textBox2.Text.ToString()) + ",'未支付')";
                sqlcon.Open();
                SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
                sqlcmd.ExecuteNonQuery();
                    MessageBox.Show("添加购物车成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString()+"\n添加购物车失败！");
                }
                finally
                {
                    sqlcon.Close();
                }
            }
        }

        private void btn_buy_Click(object sender, EventArgs e)
        {
            pay.num = textBox2.Text.ToString();
            if (textBox2.Text == "")
            {
                MessageBox.Show("请输入商品数量");
            }
            else if (num < int.Parse(textBox2.Text.ToString()))
            {
                MessageBox.Show("商品数量超出库存值！请选择合理数量");
            }
            else
            {
                pay pay = new pay();
                pay.MdiParent = login.mainform;
                pay.Show();

            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dgvRow = dataGridView1.Rows[e.RowIndex];
            DataGridViewCellCollection dgvCC = dgvRow.Cells;
            gid = dgvCC[0].Value.ToString();
            pay.gid = gid;
            price = int.Parse(dgvCC[3].Value.ToString());
            pay.price = price;
            num= int.Parse(dgvCC[4].Value.ToString());
        }

        private void add_Click(object sender, EventArgs e)
        {
            int i = int.Parse(textBox2.Text.ToString()) + 1;
            textBox2.Text = i.ToString();
        }

        private void re_Click(object sender, EventArgs e)
        {
            int i=int.Parse(textBox2.Text.ToString());
            if (i<=0)
            {
                MessageBox.Show("不能再减了！");
                return;
            }
            else
            {
                i=i-1;
                textBox2.Text = i.ToString();
            }

        }
    }
}
